package com.smart_campus.bus.controller;



import com.smart_campus.bus.domain.ModelType;
import com.smart_campus.bus.vo.JournalVo;
import com.smart_campus.sys.common.ActiveUser;
import com.smart_campus.sys.common.Constant;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.bus.domain.Journal;
import com.smart_campus.bus.service.JournalService;
import com.smart_campus.sys.common.ResultObj;
import com.smart_campus.sys.common.upload.UploadService;
import com.smart_campus.sys.domain.User;
import com.smart_campus.sys.domain.UserInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author saoji
 * @since 2020-06-07*/
@CrossOrigin
@RestController
@RequestMapping("journal")
public class JournalController {

    @Autowired
    private JournalService journalService;

    @Autowired
    private UploadService uploadService;

    /**
     * 新闻查询
     * */
    @RequestMapping("news")
    public DataGridView getNews(Integer page,Integer limit,String keyWords){
//        System.out.println("page = " + page);
//        System.out.println("limit = " + limit);
        List<Journal> news_List = journalService.getJournal(page,limit,Constant.Journal_TYPE_NEWS,keyWords);
        return new DataGridView(news_List);
    }

    /**
     * 公告查询
     * */
    @RequestMapping("notice")
    public DataGridView getNotice(Integer page,Integer limit,String keyWords){
        List<Journal> announcement_List = journalService.getJournal(page,limit,Constant.Journal_TYPE_NOTICE,keyWords);
        return new DataGridView(announcement_List);
    }

    /**
     * 活动查询
     * */
    @RequestMapping("event")
    public DataGridView getEvent(Integer page,Integer limit,String keyWords){
        List<Journal> activity_List = journalService.getJournal(page,limit,Constant.Journal_TYPE_EVENT,keyWords);
        return new DataGridView(activity_List);
    }


    /**
     * 统配查询
     *
     * 需要添加分页查询
     * */
    @RequestMapping("selOfType")
    public DataGridView getJournal(Integer page,Integer limit,String type,String keyWords){
        if (type==null||"".equals(type)){
            type= Constant.Journal_TYPE_ALL;
        }
        List<Journal> activity_List = journalService.getJournal(page,limit,type,keyWords);
        return new DataGridView(activity_List);
    }
    /**
     * 查询所有模块数据
     * */
    @RequestMapping("getModelType")
    public DataGridView getAllModelType(){
        List<ModelType> allModelType = journalService.getAllModelType();
        return new DataGridView(allModelType);
    }

    /*
     * 模块查询数据
     * */
    @RequestMapping("getDataByModel")
    public DataGridView getDataByModel(Integer page,Integer limit,String modelWords){
        List<Journal> dataByModel = journalService.getDataByModel(page, limit, modelWords);
        return new DataGridView(dataByModel);
    }
    /*
     * 热文查询
     * */
    @RequestMapping("getHotNews")
    public DataGridView getHotNews(){
        List<Journal> hotNews = journalService.getHotNews();
        return new DataGridView(hotNews);
    }

    /**
     * 根据ID查询数据
     * */
    @RequestMapping("getJournalById")
    public DataGridView getDataById(int id){
        Journal journal = journalService.getDataById(id);
        System.out.println(journal.toString());
        journal.setReadCount(journal.getReadCount()+1);
        journalService.addReadCount(journal);
        journal = journalService.getDataById(id);
        return new DataGridView(journal);
    }

    /**
     * 添加新闻
     * */
    @RequestMapping("addJournal")
    public ResultObj addJournal(Journal journal,MultipartFile mf) throws IOException {
        try {
            String path = null;
            if (mf!=null){
                path = uploadGoodsFile(mf);
            }
            journal.setCreateTime(new Date());
            journal.setHref("detail.html");
            journal.setCommentCount(0);
            journal.setReadCount(0);
            journal.setImage(path);
            journalService.addJournal(journal);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }

    }


    /**
     * 添加公告、活动
     * */
    @RequestMapping("addJournals")
    public ResultObj addJournals(Journal journal) throws IOException {
        try {
            String path = null;
            System.out.println("类型"+journal.getType());
            if (journal.getType().equals("notice")){
                path = "group1/M00/00/00/rBL3wl74CYOAUEw8AAJ31u_aTvI805.png";
            }else if (journal.getType().equals("event") ){
                path = "group1/M00/00/00/rBL3wl74DdmAYEJuAAJAqrV8X3443.jpeg";
            }
            journal.setCreateTime(new Date());
            journal.setHref("detail.html");
            journal.setCommentCount(0);
            journal.setReadCount(0);
            journal.setImage(path);
            journalService.addJournal(journal);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }

    }

    /**
     * 修改
     * */
    @RequestMapping("updateJournal")
    public ResultObj updateJournal(Journal journal){
        try {
            journalService.updateJournal(journal);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }

    }


    /**
     * 删除
     * */
    @RequestMapping("removeJournal")
    public ResultObj removeJournal(int id[]){
        try {
            for (int i:id){
                journalService.removeJournal(i);
            }
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("selallJournal")
    public DataGridView selallJournal(JournalVo vo){
        return journalService.selallJournal(vo);
    }


    public String uploadGoodsFile(MultipartFile mf){

        String path = this.uploadService.uploadImage(mf);

        Map<String,String> map=new HashMap<>();
        map.put("src",path);
        return path;
    }
}

